<!--productbatchmgr生产批次 -->
<template>
  <a-card
    :bordered="false"
    :body-style="{ 'padding-bottom': '0px' }"
    :style="{ margin: '6px', paddingBottom: '0px' }"
  >
    <a-form
      ref="searchFormRef"
      name="advanced_search"
      :model="searchFormState"
      class="ant-advanced-search-form"
    >
      <a-row :gutter="24" >
        <a-col :span="4">
          <a-form-item label="批次名称" name="PBName">
            <a-input
              v-model:value="searchFormState.PBName"
              placeholder="请输入批次名称"
              @keyup.enter="tableRef.refresh(true)"
            />
          </a-form-item>
        </a-col>

        <a-col :span="4">
          <a-form-item label="订单编号" name="PBNo">
            <a-input
              v-model:value="searchFormState.PBNo"
              placeholder="请输入订单编号"
              @keyup.enter="tableRef.refresh(true)"
            />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="创建时间" name="time">
            <a-range-picker
              v-model:value="searchFormState.time"
              showTime
              :format="dayFormat"
            />
          </a-form-item>
        </a-col>
        <a-col :span="4">
          <a-form-item label="创建人" name="UserID">
            <a-select
              v-model:value="searchFormState.UserID"
              style="width: 100%"
              placeholder="请选择创建人"
              :options="userList"
            ></a-select>
          </a-form-item>
        </a-col>
        <a-col :span="4">
          <a-form-item label="产品型号" name="PMID">
            <a-select
              v-model:value="searchFormState.PMID"
              style="width: 100%"
              placeholder="请选择产品型号"
              :options="productModel"
            ></a-select>
          </a-form-item>
        </a-col>
      </a-row>
      <a-row :gutter="24">
        <a-col :span="12">
          <a-button
            class="mr10"
            type="primary"
            @click="tableRef.refresh(true)"
            v-show="hasRole(route.meta.RolePower)"
            >查询</a-button
          >
          <a-button
            class="xn-mg08"
            @click="reset"
            v-show="hasRole(route.meta.RolePower)"
            >重置</a-button
          >
         
        </a-col>
      </a-row>
    </a-form>
  </a-card>
  <a-card :bordered="false" :style="{ margin: '6px' }">
    <m-table
      :show-pagination="true"
      ref="tableRef"
      :columns="columns"
      :data="loadData"
      :alert="options.alert.show"
      bordered
      :row-key="(record) => record.ID"
      :tool-config="toolConfig"
      :row-selection="options.rowSelection"
      :scroll="{ x: 'max-content' }"
    >
      <template #operator>
        <a-space>
          <a-button
            type="primary"
            @click="formRef.onOpen()"
            v-show="hasAddRole(route.meta.RolePower)"
          >
            <template #icon><plus-outlined /></template>
            新增
          </a-button>
        </a-space>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.dataIndex === 'CreateTime'">
          {{ transitionTime(record.CreateTime) }}
        </template>
        <template v-if="column.dataIndex === 'PBBeginTime'">
          {{ transitionTime(record.PBBeginTime) }}
        </template>
        <template v-if="column.dataIndex === 'PBEndTime'">
          {{ transitionTime(record.PBEndTime) }}
        </template>
        <template v-if="column.dataIndex === 'action'">
          <a-button
            type="link"
            @click="toBatchModel(record.ID)"
            v-show="hasEditRole(route.meta.RolePower)"
            >批次型号管理</a-button
          >
          <a-divider type="vertical" />
          <a-button
            type="link"
            @click="formRef.onOpen(record)"
            v-show="hasEditRole(route.meta.RolePower)"
            >编辑</a-button
          >
          <a-divider type="vertical" />
          <a-popconfirm
            title="确定删除？"
            @confirm="deleteModule(record)"
            ok-text="确定"
            cancel-text="关闭"
          >
            <a-button
              type="link"
              danger
              size="small"
              v-show="hasDeleteRole(route.meta.RolePower)"
              >删除</a-button
            >
          </a-popconfirm>
        </template>
      </template>
    </m-table>
  </a-card>
  <Form ref="formRef" @successful="tableRef.refresh(true)" />
</template>
    
<script setup name="productbatchmgr">
import productbatchmgrApi from "@/api/productManagement/productbatchmgrApi";
import usermgrApi from "@/api/systemManagement/usermgrApi";
import productmodelmgrApi from "@/api/productManagement/productmodelmgrApi";
import Form from "./form.vue";
import { cloneDeep } from "lodash-es";
import { message } from "ant-design-vue";
import { useRoute } from "vue-router";
const route = useRoute();
import {
  transitionTime,
  getPreviousMonthDateTime,
  getCurrentDateTime,
} from "@/utils/time.js";
import { ref, onMounted } from "vue";
import dayjs from "dayjs";
import { useRouter } from "vue-router";
const router = useRouter();

const dayFormat = "YYYY/MM/DD HH:mm:ss";

const searchFormState = ref({
  ID: 0,
  PBName: "",
  StartTime: "",
  EndTime: "",
  PBNo: "",
  UserID: 0,
  PMID: 0,
  time: [
   /*  dayjs(getPreviousMonthDateTime(), dayFormat),
    dayjs(getCurrentDateTime(), dayFormat), */
  ],
});
const formRef = ref();
const searchFormRef = ref();
const tableRef = ref();
const toolConfig = {
  refresh: true,
  height: true,
  columnSetting: false,
  striped: false,
};
const columns = [
  {
    title: "批次名称",
    dataIndex: "PBName",
    align: "center",
  },
  {
    title: "订单编号",
    dataIndex: "PBNo",
    align: "center",
  },
  {
    title: "创建时间",
    dataIndex: "CreateTime",
    align: "center",
  },
  {
    title: "创建人",
    dataIndex: "CreateUserName",
    align: "center",
  },
  {
    title: "批次开始时间",
    dataIndex: "PBBeginTime",
    align: "center",
  },
  {
    title: "批次结束时间",
    dataIndex: "PBEndTime",
    align: "center",
  },
  {
    title: "批次说明",
    dataIndex: "PBDesp",
    align: "center",
  },
  {
    title: "操作",
    dataIndex: "action",
    align: "center",
    width: "280px",
    fixed: "right",
  },
];
// 列表选择配置
const options = {
  alert: {
    show: false,
    clear: () => {},
  },
};

const loadData = (parameter) => {
  const searchFormParam = cloneDeep(searchFormState.value);
  if (JSON.stringify(searchFormParam.time) != "[]") {
    searchFormParam.StartTime = dayjs(searchFormParam.time[0]).format(
      dayFormat
    );
    searchFormParam.EndTime = dayjs(searchFormParam.time[1]).format(dayFormat);
  } /* else {
    searchFormParam.StartTime = getPreviousMonthDateTime();
  } */
  delete searchFormParam.time;

  // 把查询条件保存到导出条件中
  return productbatchmgrApi
    .getProductBatchs(Object.assign(parameter, searchFormParam))
    .then((res) => {
      return res;
    });
};

// 重置
const reset = () => {
  searchFormRef.value.resetFields();
  tableRef.value.refresh(true);
};
// 查询用户列表
const userList = ref([]);
const fetchUserList = () => {
  usermgrApi
    .getUsers({ ID: 0, DeptID: 0, RoleID: 0, UserName: "", IsEnable: -1 })
    .then((res) => {
      userList.value = res.Data.map((i) => {
        return {
          label: i.UserName,
          value: i.ID,
        };
      });
      userList.value.unshift({
        label: "全部",
        value: 0,
      });
    });
};
// 产品型号下拉框数据

let productModel = ref([]);
const fetchProductModel = () => {
  productmodelmgrApi
    .getProductModels({
      ID: 0,
      PMName: "",
      PMNo: "",
      PTID: 0,
    })
    .then((res) => {
      productModel.value = res.Data.map((i) => {
        return {
          label: i.PMName,
          value: i.ID,
        };
      });
      productModel.value.unshift({
        label: "全部",
        value: 0,
      });
    });
};
// 删除
const deleteModule = (record) => {
  productbatchmgrApi
    .deleteProductBatch({
      ID: record.ID,
      PBName: record.PBName,
    })
    .then((res) => {
      if (!res.Result) {
        message.error("删除失败");
        return;
      }
      message.success("删除成功");
      tableRef.value.refresh();
    })
    .catch(() => {
      message.error("删除失败");
    });
};

onMounted(() => {
  fetchUserList();
  fetchProductModel();
});
const toBatchModel = (id) => {
  router.push({
    path: `/productManagement/productbatchmodel`,
    query: {
      id,
    },
  });
};
</script>
    